library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
bike_data1 <- read_csv("E:/r_project/day.csv")
## Rows: 731 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl  (15): instant, season, yr, mnth, holiday, weekday, workingday, weathers...
## date  (1): dteday
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
summary(bike_data1)
##     instant          dteday               season            yr        
##  Min.   :  1.0   Min.   :2011-01-01   Min.   :1.000   Min.   :0.0000  
##  1st Qu.:183.5   1st Qu.:2011-07-02   1st Qu.:2.000   1st Qu.:0.0000  
##  Median :366.0   Median :2012-01-01   Median :3.000   Median :1.0000  
##  Mean   :366.0   Mean   :2012-01-01   Mean   :2.497   Mean   :0.5007  
##  3rd Qu.:548.5   3rd Qu.:2012-07-01   3rd Qu.:3.000   3rd Qu.:1.0000  
##  Max.   :731.0   Max.   :2012-12-31   Max.   :4.000   Max.   :1.0000  
##       mnth          holiday           weekday        workingday   
##  Min.   : 1.00   Min.   :0.00000   Min.   :0.000   Min.   :0.000  
##  1st Qu.: 4.00   1st Qu.:0.00000   1st Qu.:1.000   1st Qu.:0.000  
##  Median : 7.00   Median :0.00000   Median :3.000   Median :1.000  
##  Mean   : 6.52   Mean   :0.02873   Mean   :2.997   Mean   :0.684  
##  3rd Qu.:10.00   3rd Qu.:0.00000   3rd Qu.:5.000   3rd Qu.:1.000  
##  Max.   :12.00   Max.   :1.00000   Max.   :6.000   Max.   :1.000  
##    weathersit         temp             atemp              hum        
##  Min.   :1.000   Min.   :0.05913   Min.   :0.07907   Min.   :0.0000  
##  1st Qu.:1.000   1st Qu.:0.33708   1st Qu.:0.33784   1st Qu.:0.5200  
##  Median :1.000   Median :0.49833   Median :0.48673   Median :0.6267  
##  Mean   :1.395   Mean   :0.49538   Mean   :0.47435   Mean   :0.6279  
##  3rd Qu.:2.000   3rd Qu.:0.65542   3rd Qu.:0.60860   3rd Qu.:0.7302  
##  Max.   :3.000   Max.   :0.86167   Max.   :0.84090   Max.   :0.9725  
##    windspeed           casual         registered        cnt      
##  Min.   :0.02239   Min.   :   2.0   Min.   :  20   Min.   :  22  
##  1st Qu.:0.13495   1st Qu.: 315.5   1st Qu.:2497   1st Qu.:3152  
##  Median :0.18097   Median : 713.0   Median :3662   Median :4548  
##  Mean   :0.19049   Mean   : 848.2   Mean   :3656   Mean   :4504  
##  3rd Qu.:0.23321   3rd Qu.:1096.0   3rd Qu.:4776   3rd Qu.:5956  
##  Max.   :0.50746   Max.   :3410.0   Max.   :6946   Max.   :8714
View(bike_data1)
bike_data2 <- read_csv("E:/r_project/hour.csv")
## Rows: 17379 Columns: 17
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl  (16): instant, season, yr, mnth, hr, holiday, weekday, workingday, weat...
## date  (1): dteday
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
summary(bike_data2)
##     instant          dteday               season            yr        
##  Min.   :    1   Min.   :2011-01-01   Min.   :1.000   Min.   :0.0000  
##  1st Qu.: 4346   1st Qu.:2011-07-04   1st Qu.:2.000   1st Qu.:0.0000  
##  Median : 8690   Median :2012-01-02   Median :3.000   Median :1.0000  
##  Mean   : 8690   Mean   :2012-01-02   Mean   :2.502   Mean   :0.5026  
##  3rd Qu.:13034   3rd Qu.:2012-07-02   3rd Qu.:3.000   3rd Qu.:1.0000  
##  Max.   :17379   Max.   :2012-12-31   Max.   :4.000   Max.   :1.0000  
##       mnth              hr           holiday           weekday     
##  Min.   : 1.000   Min.   : 0.00   Min.   :0.00000   Min.   :0.000  
##  1st Qu.: 4.000   1st Qu.: 6.00   1st Qu.:0.00000   1st Qu.:1.000  
##  Median : 7.000   Median :12.00   Median :0.00000   Median :3.000  
##  Mean   : 6.538   Mean   :11.55   Mean   :0.02877   Mean   :3.004  
##  3rd Qu.:10.000   3rd Qu.:18.00   3rd Qu.:0.00000   3rd Qu.:5.000  
##  Max.   :12.000   Max.   :23.00   Max.   :1.00000   Max.   :6.000  
##    workingday       weathersit         temp           atemp       
##  Min.   :0.0000   Min.   :1.000   Min.   :0.020   Min.   :0.0000  
##  1st Qu.:0.0000   1st Qu.:1.000   1st Qu.:0.340   1st Qu.:0.3333  
##  Median :1.0000   Median :1.000   Median :0.500   Median :0.4848  
##  Mean   :0.6827   Mean   :1.425   Mean   :0.497   Mean   :0.4758  
##  3rd Qu.:1.0000   3rd Qu.:2.000   3rd Qu.:0.660   3rd Qu.:0.6212  
##  Max.   :1.0000   Max.   :4.000   Max.   :1.000   Max.   :1.0000  
##       hum           windspeed          casual         registered   
##  Min.   :0.0000   Min.   :0.0000   Min.   :  0.00   Min.   :  0.0  
##  1st Qu.:0.4800   1st Qu.:0.1045   1st Qu.:  4.00   1st Qu.: 34.0  
##  Median :0.6300   Median :0.1940   Median : 17.00   Median :115.0  
##  Mean   :0.6272   Mean   :0.1901   Mean   : 35.68   Mean   :153.8  
##  3rd Qu.:0.7800   3rd Qu.:0.2537   3rd Qu.: 48.00   3rd Qu.:220.0  
##  Max.   :1.0000   Max.   :0.8507   Max.   :367.00   Max.   :886.0  
##       cnt       
##  Min.   :  1.0  
##  1st Qu.: 40.0  
##  Median :142.0  
##  Mean   :189.5  
##  3rd Qu.:281.0  
##  Max.   :977.0
View(bike_data2)

options(repos = c(CRAN = "https://cran.r-project.org/mirrors.html/?mirror=<43>"))



install.packages("timetk")
## Installing package into 'C:/Users/kartik/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## Warning: unable to access index for repository https://cran.r-project.org/mirrors.html/?mirror=<43>/src/contrib:
##   cannot open URL 'https://cran.r-project.org/mirrors.html/?mirror=<43>/src/contrib/PACKAGES'
## Warning: package 'timetk' is not available for this version of R
## 
## A version of this package for your version of R might be available elsewhere,
## see the ideas at
## https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
## Warning: unable to access index for repository https://cran.r-project.org/mirrors.html/?mirror=<43>/bin/windows/contrib/4.3:
##   cannot open URL 'https://cran.r-project.org/mirrors.html/?mirror=<43>/bin/windows/contrib/4.3/PACKAGES'
library(timetk)
data(bike_sharing_daily)
View(bike_sharing_daily)

# Calculate correlation
correlation <- cor(bike_sharing_daily$temp, bike_sharing_daily$cnt)
correlation
## [1] 0.627494
# Calculate mean and median temperatures by season
temp_summary <- aggregate(bike_sharing_daily$temp, by = list(bike_sharing_daily$season), FUN = function(x) c(Mean_Temperature = mean(x), Median_Temperature = median(x)))
temp_summary
##   Group.1 x.Mean_Temperature x.Median_Temperature
## 1       1          0.2977475            0.2858330
## 2       2          0.5444052            0.5620835
## 3       3          0.7063093            0.7145830
## 4       4          0.4229060            0.4091665
# Calculate mean values per month
month_summary <- aggregate(bike_sharing_daily[, c("temp", "hum", "windspeed", "cnt")], by = list(bike_sharing_daily$mnth), FUN = mean)
colnames(month_summary) <- c("Month", "Mean_Temperature", "Mean_Humidity", "Mean_WindSpeed", "Mean_TotalRentals")
month_summary
##    Month Mean_Temperature Mean_Humidity Mean_WindSpeed Mean_TotalRentals
## 1      1        0.2364439     0.5858283      0.2063028          2176.339
## 2      2        0.2992264     0.5674647      0.2156839          2655.298
## 3      3        0.3905388     0.5884750      0.2226994          3692.258
## 4      4        0.4699988     0.5880631      0.2344822          4484.900
## 5      5        0.5947984     0.6889583      0.1829889          5349.774
## 6      6        0.6840972     0.5758055      0.1854199          5772.367
## 7      7        0.7554704     0.5978763      0.1660588          5563.677
## 8      8        0.7085816     0.6377301      0.1729181          5664.419
## 9      9        0.6164850     0.7147144      0.1659451          5766.517
## 10    10        0.4850122     0.6937609      0.1752055          5199.226
## 11    11        0.3692198     0.6248765      0.1838014          4247.183
## 12    12        0.3240310     0.6660405      0.1766089          3403.806
# Scatterplot for registered rentals
plot(bike_sharing_daily$temp, bike_sharing_daily$registered, xlab = "Temperature", ylab = "Registered Rentals", main = "Temperature vs. Registered Rentals")

# Scatterplot for casual rentals
plot(bike_sharing_daily$temp, bike_sharing_daily$casual, xlab = "Temperature", ylab = "Casual Rentals", main = "Temperature vs. Casual Rentals")

# Create a boxplot
boxplot(bike_sharing_daily$temp ~ bike_sharing_daily$season, xlab = "Season", ylab = "Temperature", main = "Temperature Change Across Seasons")

library(timetk)
library(lubridate)
## 
## Attaching package: 'lubridate'
## 
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
# Create interactive time series plot by year
bike_sharing_daily %>%
  tk_tbl() %>%
  mutate(Year = year(dteday)) %>%
  plot_time_series(.date = dteday, .value = cnt, .facet_var = "Year", .interactive = TRUE, .plotly_slider = TRUE)
## Warning in tk_tbl.data.frame(.): Warning: No index to preserve. Object
## otherwise converted to tibble successfully.
bike_sharing_daily %>%
  tk_tbl() %>%
  plot_seasonal_diagnostics(.date = dteday, .value = cnt)
## Warning in tk_tbl.data.frame(.): Warning: No index to preserve. Object
## otherwise converted to tibble successfully.
bike_sharing_daily %>%
  tk_tbl() %>%
  plot_anomaly_diagnostics(.date = dteday, .value = cnt)
## Warning in tk_tbl.data.frame(.): Warning: No index to preserve. Object
## otherwise converted to tibble successfully.
## frequency = 7 observations per 1 week
## trend = 92 observations per 3 months
library(stats)
library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
# Create a time series object from the count data
ts_data <- ts(bike_sharing_daily$cnt, frequency = 7)  # Assuming daily data with a weekly seasonality

# Clean the time series data to handle outliers and missing values
cleaned_ts <- tsclean(ts_data)

# Fit Simple Exponential Smoothing (Holt-Winters) to the cleaned data
smoothed_ses <- HoltWinters(cleaned_ts, beta = FALSE, gamma = FALSE)

# Print the smoothed values
print(smoothed_ses)
## Holt-Winters exponential smoothing without trend and without seasonal component.
## 
## Call:
## HoltWinters(x = cleaned_ts, beta = FALSE, gamma = FALSE)
## 
## Smoothing parameters:
##  alpha: 0.2873318
##  beta : FALSE
##  gamma: FALSE
## 
## Coefficients:
##       [,1]
## a 2121.138
install.packages("zoo")
## Installing package into 'C:/Users/kartik/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## Warning: unable to access index for repository https://cran.r-project.org/mirrors.html/?mirror=<43>/src/contrib:
##   cannot open URL 'https://cran.r-project.org/mirrors.html/?mirror=<43>/src/contrib/PACKAGES'
## Warning: package 'zoo' is not available for this version of R
## 
## A version of this package for your version of R might be available elsewhere,
## see the ideas at
## https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
## Warning: unable to access index for repository https://cran.r-project.org/mirrors.html/?mirror=<43>/bin/windows/contrib/4.3:
##   cannot open URL 'https://cran.r-project.org/mirrors.html/?mirror=<43>/bin/windows/contrib/4.3/PACKAGES'
library(zoo)
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
smoothed_sma <- rollmean(cleaned_ts, k = 10, fill = NA)
print(smoothed_sma)
## Time Series:
## Start = c(1, 1) 
## End = c(105, 3) 
## Frequency = 7 
##   [1]       NA       NA       NA       NA 1251.500 1279.300 1315.400 1321.100
##   [9] 1307.000 1271.800 1231.600 1180.600 1153.000 1235.800 1296.400 1324.400
##  [17] 1306.300 1264.300 1263.800 1337.500 1267.700 1210.800 1259.200 1204.000
##  [25] 1120.900 1116.700 1154.600 1208.600 1222.000 1194.300 1244.200 1363.400
##  [33] 1417.900 1461.100 1512.000 1515.700 1554.300 1548.900 1552.800 1573.300
##  [41] 1654.300 1703.500 1779.800 1919.500 1922.500 1949.900 1886.000 1883.800
##  [49] 1916.600 1906.000 1870.600 1856.000 1848.700 1700.600 1722.200 1754.400
##  [57] 1812.200 1861.600 1877.600 1757.400 1798.500 1814.900 1763.800 1681.500
##  [65] 1694.100 1693.900 1767.100 1777.300 1775.200 1933.900 2021.100 2131.700
##  [73] 2254.300 2439.100 2449.100 2506.200 2476.600 2458.500 2473.900 2504.300
##  [81] 2399.200 2278.100 2208.900 2115.400 2076.200 2028.600 2041.700 2180.100
##  [89] 2270.600 2200.500 2312.000 2423.300 2327.900 2419.800 2540.800 2652.900
##  [97] 2631.100 2522.400 2537.600 2670.700 2469.400 2529.700 2725.500 2800.400
## [105] 2905.300 2989.400 2954.300 3141.700 3234.100 3328.800 3689.300 3702.100
## [113] 3765.000 3904.100 4040.900 3957.100 4228.900 4270.400 4114.600 4150.600
## [121] 4171.400 4255.600 4283.100 4259.800 4208.900 4292.000 4338.300 4303.700
## [129] 4381.300 4393.300 4328.300 4269.200 4221.400 4242.700 4254.100 4416.400
## [137] 4396.000 4412.900 4521.200 4563.700 4635.600 4691.200 4781.500 4802.800
## [145] 4720.900 4538.600 4470.000 4539.400 4621.400 4657.800 4680.700 4667.600
## [153] 4675.100 4636.400 4618.100 4678.500 4777.700 4726.900 4697.700 4652.600
## [161] 4680.000 4601.900 4603.000 4674.800 4757.700 4700.100 4687.000 4691.700
## [169] 4668.700 4678.700 4680.900 4834.700 4821.100 4774.000 4822.100 4972.600
## [177] 5025.300 5086.500 5072.400 5177.600 5123.900 5056.300 5044.700 4983.900
## [185] 4995.000 4931.600 4804.000 4717.900 4687.200 4591.300 4678.600 4808.000
## [193] 4879.000 4920.800 4841.300 4786.400 4756.200 4669.100 4563.400 4415.600
## [201] 4245.800 4112.500 4047.900 4041.100 3971.600 3985.900 4037.700 4125.600
## [209] 4281.600 4278.400 4352.000 4379.600 4343.400 4282.900 4330.900 4343.600
## [217] 4391.400 4444.000 4450.000 4507.600 4432.000 4379.200 4422.300 4513.200
## [225] 4461.100 4416.200 4457.300 4365.400 4350.700 4525.200 4656.200 4576.600
## [233] 4570.200 4212.300 4265.200 4313.300 4314.600 4433.100 4468.800 4352.000
## [241] 4287.400 4427.200 4296.200 4455.700 4221.900 3942.700 3776.700 3805.400
## [249] 3798.500 3797.100 3825.000 3809.500 3840.300 4045.300 4296.800 4540.000
## [257] 4639.500 4469.100 4399.700 4407.900 4171.100 4234.900 4370.000 4357.000
## [265] 4317.900 4281.200 4311.200 4467.300 4275.000 4087.300 4204.800 4108.100
## [273] 4089.700 4103.200 4189.700 4339.900 4407.100 4398.600 4612.000 4561.800
## [281] 4496.100 4414.900 4454.000 4481.600 4440.100 4374.000 4065.300 3973.100
## [289] 3947.200 4136.400 4283.200 4337.500 4284.500 4169.800 3978.700 3878.600
## [297] 3698.900 3612.500 3549.000 3525.000 3505.500 3484.200 3420.100 3423.300
## [305] 3522.300 3551.100 3908.900 3986.700 3913.100 3843.100 3831.200 3805.500
## [313] 3849.500 3876.400 3693.200 3595.000 3513.700 3469.100 3527.800 3467.500
## [321] 3221.500 3106.400 2807.300 2667.000 2792.100 2793.900 2841.400 2766.500
## [329] 2775.800 2872.000 3105.300 3210.100 3409.100 3511.000 3463.600 3227.000
## [337] 3172.500 3243.100 3200.800 3102.400 3039.400 3030.300 3055.800 3045.600
## [345] 3143.900 3347.300 3258.200 3236.500 3292.500 3284.200 3260.000 3128.600
## [353] 2855.700 2560.200 2334.200 2176.500 2163.600 2065.600 1990.500 1973.000
## [361] 1895.600 1869.800 1992.300 2153.700 2349.200 2642.800 2864.700 2964.900
## [369] 2902.600 3013.900 3002.200 3216.800 3314.600 3327.100 3231.000 3051.000
## [377] 2892.400 2887.500 2979.100 2935.600 2848.000 2636.000 2557.800 2742.400
## [385] 2938.300 3116.000 3168.100 3232.800 3227.900 3274.000 3594.800 3855.000
## [393] 3987.900 3969.100 3825.300 3712.500 3745.300 3780.500 3736.400 3757.000
## [401] 3689.200 3448.200 3225.000 3152.100 3261.100 3383.300 3305.400 3283.300
## [409] 3434.900 3320.800 3250.600 3411.400 3735.800 3899.800 3856.300 3712.600
## [417] 3751.000 3767.800 3772.300 3686.800 3872.900 3814.600 3743.900 3580.000
## [425] 3564.600 3687.000 3839.700 3945.700 3966.300 4194.700 4186.800 4397.200
## [433] 4575.300 4864.200 5150.100 5192.300 5484.300 5535.300 5693.700 5891.200
## [441] 6023.100 6180.400 6431.900 6137.900 6018.300 6136.300 5862.900 5843.500
## [449] 5841.500 5778.100 5778.600 5695.600 5453.000 5793.000 5937.000 6026.900
## [457] 6162.700 6278.600 6182.200 6194.800 6163.100 6045.200 5992.500 5955.100
## [465] 6057.500 6125.000 6116.000 6099.400 6019.200 6117.200 6254.400 6430.600
## [473] 6353.948 6035.548 5852.848 5759.248 5624.848 5579.048 5564.348 5538.248
## [481] 5366.448 5278.048 5430.700 5751.400 5817.700 5886.400 6019.700 6023.700
## [489] 6174.500 6015.800 6115.800 6244.800 6370.800 6340.500 6254.866 6078.066
## [497] 6184.566 6295.666 6486.766 6844.466 6900.166 6633.066 6497.466 6411.666
## [505] 6544.700 6706.600 6617.800 6538.500 6378.900 6123.800 6096.400 6394.300
## [513] 6199.700 6485.700 6572.800 6599.200 6645.700 6692.100 6837.200 7036.500
## [521] 7100.800 7026.800 7280.500 6965.700 6943.700 6980.200 7046.600 7111.300
## [529] 7059.700 6796.000 6728.700 6690.000 6614.100 6699.200 6702.900 6655.700
## [537] 6567.100 6541.100 6576.800 6754.800 6618.600 6566.200 6528.800 6569.200
## [545] 6489.400 6540.600 6486.800 6363.300 6113.800 5893.100 6003.700 6064.000
## [553] 6237.300 6359.200 6443.100 6399.700 6378.700 6441.000 6635.600 6739.700
## [561] 6741.900 6699.900 6419.400 6415.800 6362.500 6424.800 6639.000 6642.100
## [569] 6653.900 6751.100 6751.700 6875.200 7150.900 7167.900 7197.400 7155.700
## [577] 7020.800 6881.100 6892.000 6950.800 7044.500 7062.600 6919.600 6791.500
## [585] 6719.800 6690.600 6686.600 6874.900 6934.100 6921.600 6954.700 6681.000
## [593] 6755.400 6826.100 6909.200 6997.400 7077.200 6947.800 6712.800 6689.700
## [601] 6607.200 6922.000 7040.300 7074.700 6951.200 6755.700 6600.900 6682.000
## [609] 6867.700 6796.300 6842.700 6670.600 6722.000 6739.500 6902.200 7108.200
## [617] 7285.200 7399.700 7559.900 7672.900 7609.400 7419.100 7355.500 7375.000
## [625] 7415.000 7467.500 7477.800 7420.500 7302.900 7342.900 7395.300 7729.500
## [633] 7825.900 7742.800 7603.900 7228.300 7194.800 7184.000 7245.800 7269.000
## [641] 7141.883 6948.183 6731.883 6812.083 6891.283 7155.583 7109.283 7040.383
## [649] 6812.283 6769.183 6903.100 7106.200 7009.400 7049.300 6974.700 6952.300
## [657] 6988.000 7093.400 7241.800 7232.800 7271.900 6966.900 6921.135 6660.202
## [665] 6534.402 6427.202 6265.302 6009.802 5784.602 5566.102 5349.502 5407.102
## [673] 5441.966 5493.100 5590.100 5676.700 5718.900 5614.500 5653.300 5671.900
## [681] 5673.100 5732.500 5667.900 5618.600 5528.400 5357.800 4973.400 4955.000
## [689] 4633.200 4331.100 4270.000 4103.000 4162.100 4144.500 4147.900 4152.400
## [697] 4374.800 4607.200 5040.100 5370.600 5399.400 5504.300 5536.500 5327.000
## [705] 5277.200 5308.200 5375.200 5305.000 5205.500 5137.300 4978.400 4936.100
## [713] 4933.600 5137.500 5033.300 4845.500 4488.500 4114.000 3644.900 3241.500
## [721] 2907.000 2659.900 2413.700 2021.100 1787.900 1698.500       NA       NA
## [729]       NA       NA       NA
# Decompose the time series data
decomposition <- decompose(cleaned_ts)

# Plot the decomposed components
plot(decomposition)

install.packages("tseries")
## Installing package into 'C:/Users/kartik/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## Warning: unable to access index for repository https://cran.r-project.org/mirrors.html/?mirror=<43>/src/contrib:
##   cannot open URL 'https://cran.r-project.org/mirrors.html/?mirror=<43>/src/contrib/PACKAGES'
## Warning: package 'tseries' is not available for this version of R
## 
## A version of this package for your version of R might be available elsewhere,
## see the ideas at
## https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
## Warning: unable to access index for repository https://cran.r-project.org/mirrors.html/?mirror=<43>/bin/windows/contrib/4.3:
##   cannot open URL 'https://cran.r-project.org/mirrors.html/?mirror=<43>/bin/windows/contrib/4.3/PACKAGES'
library(tseries)

# Perform ADF test for stationarity
adf_test <- adf.test(cleaned_ts)
print(adf_test)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  cleaned_ts
## Dickey-Fuller = -1.311, Lag order = 9, p-value = 0.8699
## alternative hypothesis: stationary
# Plot ACF and PACF to identify seasonality
acf(cleaned_ts)

pacf(cleaned_ts)

# Perform differencing to make the data stationary
differenced_ts <- diff(cleaned_ts, differences = 1)  


install.packages("forecast")
## Warning: package 'forecast' is in use and will not be installed
library(forecast)

# Fit a manual ARIMA model
manual_arima <- arima(differenced_ts, order = c(1, 1, 1))
print(manual_arima)
## 
## Call:
## arima(x = differenced_ts, order = c(1, 1, 1))
## 
## Coefficients:
##           ar1      ma1
##       -0.3453  -1.0000
## s.e.   0.0348   0.0038
## 
## sigma^2 estimated as 871049:  log likelihood = -6023.49,  aic = 12052.99
# Fit an auto ARIMA model
auto_arima <- auto.arima(differenced_ts)
print(auto_arima)
## Series: differenced_ts 
## ARIMA(1,0,1) with zero mean 
## 
## Coefficients:
##          ar1      ma1
##       0.3056  -0.8658
## s.e.  0.0449   0.0226
## 
## sigma^2 = 735728:  log likelihood = -5965.9
## AIC=11937.81   AICc=11937.84   BIC=11951.58
# Check residuals for manual ARIMA model
manual_residuals <- residuals(manual_arima)
shapiro_test_manual <- shapiro.test(manual_residuals)
print(shapiro_test_manual)
## 
##  Shapiro-Wilk normality test
## 
## data:  manual_residuals
## W = 0.96931, p-value = 3.034e-11
# Plot ACF and PACF for residuals
acf(manual_residuals)

pacf(manual_residuals)

# Forecast the next 25 observations with manual ARIMA
manual_forecast <- forecast(manual_arima, h = 25)
print(manual_forecast)
##          Point Forecast     Lo 80     Hi 80     Lo 95    Hi 95
## 105.4286    -319.257026 -1516.148  877.6343 -2149.744 1511.230
## 105.5714     113.085261 -1152.597 1378.7674 -1822.609 2048.779
## 105.7143     -36.181103 -1309.999 1237.6365 -1984.317 1911.955
## 105.8571      15.353175 -1259.367 1290.0737 -1934.164 1964.870
## 106.0000      -2.439057 -1277.289 1272.4110 -1952.154 1947.276
## 106.1429       3.703719 -1271.154 1278.5616 -1946.023 1953.431
## 106.2857       1.582923 -1273.279 1276.4444 -1948.149 1951.315
## 106.4286       2.315128 -1272.546 1277.1761 -1947.416 1952.047
## 106.5714       2.062334 -1272.799 1276.9236 -1947.670 1951.794
## 106.7143       2.149611 -1272.712 1277.0108 -1947.582 1951.881
## 106.8571       2.119479 -1272.742 1276.9807 -1947.612 1951.851
## 107.0000       2.129882 -1272.731 1276.9910 -1947.602 1951.862
## 107.1429       2.126290 -1272.735 1276.9875 -1947.606 1951.858
## 107.2857       2.127531 -1272.734 1276.9887 -1947.604 1951.859
## 107.4286       2.127102 -1272.734 1276.9883 -1947.605 1951.859
## 107.5714       2.127250 -1272.734 1276.9884 -1947.605 1951.859
## 107.7143       2.127199 -1272.734 1276.9884 -1947.605 1951.859
## 107.8571       2.127217 -1272.734 1276.9884 -1947.605 1951.859
## 108.0000       2.127211 -1272.734 1276.9884 -1947.605 1951.859
## 108.1429       2.127213 -1272.734 1276.9884 -1947.605 1951.859
## 108.2857       2.127212 -1272.734 1276.9884 -1947.605 1951.859
## 108.4286       2.127212 -1272.734 1276.9884 -1947.605 1951.859
## 108.5714       2.127212 -1272.734 1276.9884 -1947.605 1951.859
## 108.7143       2.127212 -1272.734 1276.9884 -1947.605 1951.859
## 108.8571       2.127212 -1272.734 1276.9884 -1947.605 1951.859
# Forecast the next 25 observations with auto ARIMA
auto_forecast <- forecast(auto_arima, h = 25)
print(auto_forecast)
##          Point Forecast     Lo 80     Hi 80     Lo 95    Hi 95
## 105.4286  -1.233797e+02 -1222.625  975.8661 -1804.531 1557.772
## 105.5714  -3.769904e+01 -1297.717 1222.3191 -1964.731 1889.332
## 105.7143  -1.151906e+01 -1285.512 1262.4741 -1959.923 1936.885
## 105.8571  -3.519685e+00 -1278.810 1271.7704 -1953.908 1946.868
## 106.0000  -1.075451e+00 -1276.487 1274.3357 -1951.648 1949.498
## 106.1429  -3.286073e-01 -1275.751 1275.0938 -1950.919 1950.262
## 106.2857  -1.004070e-01 -1275.524 1275.3231 -1950.692 1950.491
## 106.4286  -3.067966e-02 -1275.454 1275.3929 -1950.623 1950.561
## 106.5714  -9.374267e-03 -1275.433 1275.4142 -1950.601 1950.583
## 106.7143  -2.864337e-03 -1275.426 1275.4207 -1950.595 1950.589
## 106.8571  -8.752069e-04 -1275.424 1275.4227 -1950.593 1950.591
## 107.0000  -2.674222e-04 -1275.424 1275.4233 -1950.592 1950.592
## 107.1429  -8.171169e-05 -1275.424 1275.4235 -1950.592 1950.592
## 107.2857  -2.496726e-05 -1275.424 1275.4236 -1950.592 1950.592
## 107.4286  -7.628824e-06 -1275.424 1275.4236 -1950.592 1950.592
## 107.5714  -2.331011e-06 -1275.424 1275.4236 -1950.592 1950.592
## 107.7143  -7.122477e-07 -1275.424 1275.4236 -1950.592 1950.592
## 107.8571  -2.176295e-07 -1275.424 1275.4236 -1950.592 1950.592
## 108.0000  -6.649737e-08 -1275.424 1275.4236 -1950.592 1950.592
## 108.1429  -2.031848e-08 -1275.424 1275.4236 -1950.592 1950.592
## 108.2857  -6.208374e-09 -1275.424 1275.4236 -1950.592 1950.592
## 108.4286  -1.896988e-09 -1275.424 1275.4236 -1950.592 1950.592
## 108.5714  -5.796306e-10 -1275.424 1275.4236 -1950.592 1950.592
## 108.7143  -1.771079e-10 -1275.424 1275.4236 -1950.592 1950.592
## 108.8571  -5.411588e-11 -1275.424 1275.4236 -1950.592 1950.592
install.packages("rmarkdown")
## Installing package into 'C:/Users/kartik/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## Warning: unable to access index for repository https://cran.r-project.org/mirrors.html/?mirror=<43>/src/contrib:
##   cannot open URL 'https://cran.r-project.org/mirrors.html/?mirror=<43>/src/contrib/PACKAGES'
## Warning: package 'rmarkdown' is not available for this version of R
## 
## A version of this package for your version of R might be available elsewhere,
## see the ideas at
## https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
## Warning: unable to access index for repository https://cran.r-project.org/mirrors.html/?mirror=<43>/bin/windows/contrib/4.3:
##   cannot open URL 'https://cran.r-project.org/mirrors.html/?mirror=<43>/bin/windows/contrib/4.3/PACKAGES'
library(rmarkdown)